﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Newtonsoft.Json;
using Unknown.Framework.Develop;

namespace Unknown.Framework.Transform
{
    /// <summary>
    /// 对象处理类
    /// </summary>
    public static class ObjectHandler
    {
        /// <summary>
        /// 编码XML
        /// </summary>
        /// <param name="value">对象实例</param>
        /// <param name="type">类型</param>
        /// <param name="settings">设置</param>
        /// <param name="dump">是否转储</param>
        /// <returns>XML</returns>
        public static string SerializeXml(object value, Type type, XmlWriterSettings settings, bool dump)
        {
            string result = null;

            if ((value != null) && (type != null))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(type);
                XmlWriter xmlWriter = null;
                MemoryStream memoryStream = null;

                try
                {
                    memoryStream = new MemoryStream();
                    if (settings == null)
                    {
                        settings = new XmlWriterSettings();
                        settings.Indent = true;
                    }
                    xmlWriter = XmlWriter.Create(memoryStream, settings);

                    xmlSerializer.Serialize(xmlWriter, value);

                    byte[] buffer = new byte[memoryStream.Length];
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    memoryStream.Read(buffer, 0, buffer.Length);
                    result = UTF8Encoding.UTF8.GetString(buffer);
                }
                catch (ArgumentNullException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (ArgumentOutOfRangeException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (DecoderFallbackException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (ArgumentException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (ObjectDisposedException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (InvalidOperationException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                finally
                {
                    if (memoryStream != null)
                    {
                        memoryStream.Close();
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 编码XML
        /// 默认转储
        /// </summary>
        /// <param name="value">对象实例</param>
        /// <param name="type">类型</param>
        /// <param name="settings">设置</param>
        /// <returns>XML</returns>
        public static string SerializeXml(object value, Type type, XmlWriterSettings settings)
        {
            string result = null;

            result = ObjectHandler.SerializeXml(value, type, settings, true);

            return result;
        }

        /// <summary>
        /// 编码XML
        /// </summary>
        /// <param name="value">对象实例</param>
        /// <param name="type">类型</param>
        /// <param name="dump">是否转储</param>
        /// <returns>XML</returns>
        public static string SerializeXml(object value, Type type, bool dump)
        {
            string result = null;

            result = ObjectHandler.SerializeXml(value, type, null, dump);

            return result;
        }

        /// <summary>
        /// 编码XML
        /// 默认转储
        /// </summary>
        /// <param name="value">对象实例</param>
        /// <param name="type">类型</param>
        /// <returns>XML</returns>
        public static string SerializeXml(object value, Type type)
        {
            string result = null;

            result = ObjectHandler.SerializeXml(value, type, null, true);

            return result;
        }

        /// <summary>
        /// 解码XML
        /// </summary>
        /// <param name="value">XML</param>
        /// <param name="type">类型</param>
        /// <param name="settings">设置</param>
        /// <param name="dump">是否转储</param>
        /// <returns>对象实例</returns>
        public static object DeserializeXml(string value, Type type, XmlReaderSettings settings, bool dump)
        {
            object result = null;

            if ((!string.IsNullOrEmpty(value)) && (type != null))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(type);
                XmlReader xmlReader = null;
                MemoryStream memoryStream = null;
                try
                {
                    byte[] buffer = UTF8Encoding.UTF8.GetBytes(value);

                    memoryStream = new MemoryStream();
                    memoryStream.Write(buffer, 0, buffer.Length);
                    memoryStream.Seek(0, SeekOrigin.Begin);

                    if (settings == null)
                    {
                        settings = new XmlReaderSettings();
                    }

                    xmlReader = XmlReader.Create(memoryStream, settings);
                    result = xmlSerializer.Deserialize(xmlReader);
                }
                catch (ArgumentOutOfRangeException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (ArgumentNullException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (EncoderFallbackException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (ArgumentException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (IOException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (ObjectDisposedException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (InvalidOperationException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                catch (NotSupportedException exception)
                {
                    if (dump)
                    {
                        CrashHandlers.Default.Hibernate(exception);
                    }
                }
                finally
                {
                    if (memoryStream != null)
                    {
                        memoryStream.Close();
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 解码XML
        /// 默认转储
        /// </summary>
        /// <param name="value">XML</param>
        /// <param name="type">类型</param>
        /// <param name="settings">设置</param>
        /// <returns>对象实例</returns>
        public static object DeserializeXml(string value, Type type, XmlReaderSettings settings)
        {
            object result = null;

            result = ObjectHandler.DeserializeXml(value, type, settings, true);

            return result;
        }

        /// <summary>
        /// 解码XML
        /// </summary>
        /// <param name="value">XML</param>
        /// <param name="type">类型</param>
        /// <param name="dump">是否转储</param>
        /// <returns>对象实例</returns>
        public static object DeserializeXml(string value, Type type, bool dump)
        {
            object result = null;

            result = ObjectHandler.DeserializeXml(value, type, null, dump);

            return result;
        }

        /// <summary>
        /// 解码XML
        /// 默认转储
        /// </summary>
        /// <param name="value">XML</param>
        /// <param name="type">类型</param>
        /// <returns>对象实例</returns>
        public static object DeserializeXml(string value, Type type)
        {
            object result = null;

            result = ObjectHandler.DeserializeXml(value, type, null, true);

            return result;
        }

        /// <summary>
        /// 编码JSON
        /// </summary>
        /// <param name="value">对象实例</param>
        /// <param name="type">类型</param>
        /// <param name="settings">设置</param>
        /// <returns>JSON</returns>
        public static string SerializeJson(object value, Type type, JsonSerializerSettings settings)
        {
            string result = null;

            if ((value != null) && (type != null))
            {
                if (settings == null)
                {
                    settings = new JsonSerializerSettings();
                }
                result = JsonConvert.SerializeObject(value, type, settings);
            }

            return result;
        }

        /// <summary>
        /// 编码JSON
        /// </summary>
        /// <param name="value">对象实例</param>
        /// <param name="type">类型</param>
        /// <returns>JSON</returns>
        public static string SerializeJson(object value, Type type)
        {
            string result = null;

            result = ObjectHandler.SerializeJson(value, type, null);

            return result;
        }

        /// <summary>
        /// 解码JSON
        /// </summary>
        /// <param name="value">JSON</param>
        /// <param name="type">类型</param>
        /// <param name="settings">设置</param>
        /// <returns>对象实例</returns>
        public static object DeserializeJson(string value, Type type, JsonSerializerSettings settings)
        {
            object result = null;

            if ((!string.IsNullOrEmpty(value)) && (type != null))
            {
                if (settings == null)
                {
                    settings = new JsonSerializerSettings();
                }
                result = JsonConvert.DeserializeObject(value, type, settings);
            }

            return result;
        }

        /// <summary>
        /// 解码JSON
        /// </summary>
        /// <param name="value">JSON</param>
        /// <param name="type">类型</param>
        /// <returns>对象实例</returns>
        public static object DeserializeJson(string value, Type type)
        {
            object result = null;

            result = ObjectHandler.DeserializeJson(value, type, null);

            return result;
        }
    }
}
